﻿using System;
using System.Threading.Tasks;
using GBT.ParamEntity;
using GBT.SystemEnties;
using Microsoft.AspNetCore.Mvc;
using NLog;
using PersistenceDataLogic.Core;
using QueryDataLogic.Core;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebAPI_QiSiMealSystem.Controllers
{
    [Route("gbt/[controller]")]
    [ApiController]
    public class UserStoredValueController : ControllerBase
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        UserStoredValueManage userStoredValueManage = new UserStoredValueManage();
        OpterationUserStoredValue operationUserStoredValue = new OpterationUserStoredValue();

        // POST: gbt/UserStoredValue/GetUserStoredValueRecord
        [HttpPost("GetUserStoredValueRecord")]
        public async Task<IActionResult> GetUserStoredValueRecord([FromBody] System_UserStoredValueRecord value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                int totalNumber = 0;
                int totalPage = 0;
                string Error = "";
                object result = null;
                try
                {
                    result = userStoredValueManage.GetUserStoredValueRecord(value, ref totalNumber, ref totalPage);
                }
                catch (Exception ex)
                {
                    Error = ex.Message + Environment.NewLine + ex.StackTrace;
                    logger.Error(ex);
                }
                return Ok(new { data = result, Error, totalNumber, totalPage });
            });
            return task;
        }

        // POST: gbt/UserStoredValue/GetUserXpenseTracker
        [HttpPost("GetUserXpenseTracker")]
        public async Task<IActionResult> GetUserXpenseTracker([FromBody] System_UserXpenseTracker value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                int totalNumber = 0;
                int totalPage = 0;
                string Error = "";
                object result = null;
                try
                {
                    result = userStoredValueManage.GetUserXpenseTracker(value, ref totalNumber, ref totalPage);
                }
                catch (Exception ex)
                {
                    Error = ex.Message + Environment.NewLine + ex.StackTrace;
                    logger.Error(ex);
                }
                return Ok(new { data = result, Error, totalNumber, totalPage });
            });
            return task;
        }

        // POST: gbt/UserStoredValue/OptUserStoredValue
        [HttpPost("OptUserStoredValue")]
        public async Task<IActionResult> OptUserStoredValue([FromBody] System_UserStoredValue value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                string Error = "";
                string msg = "";
                int result = 0;
                try
                {
                    result = operationUserStoredValue.OptUserStoredValue(value, ref msg);
                }
                catch (Exception ex)
                {
                    Error = ex.Message + Environment.NewLine + ex.StackTrace;
                    logger.Error(ex);
                }
                return Ok(new { root = result, msg, Error });
            });
            return task;
        }

        // POST: gbt/UserStoredValue/UndoReload
        [HttpPost("UndoReload")]
        public async Task<IActionResult> UndoReload([FromBody] System_UserStoredValueRecord value)
        {
            OkObjectResult task = await Task.Run(() =>
            {
                string Error = "";
                string msg = "";
                int result = 0;
                try
                {
                    result = operationUserStoredValue.UndoReload(value, ref msg);
                }
                catch (Exception ex)
                {
                    Error = ex.Message + Environment.NewLine + ex.StackTrace;
                    logger.Error(ex);
                }
                return Ok(new { root = result, msg, Error });
            });
            return task;
        }
    }
}
